/**
 * Created with JetBrains PhpStorm.
 * User: power_yin
 * Date: 13-10-3
 * Time: 下午1:47
 * To change this template use File | Settings | File Templates.
 */
(function(xf){
    /**
     * ScrollSliderNavbar constructor (btnWarpInstance_jQ: jQueryObject, btnWarpInstance_jQ: jQueryObject, [setting HashObject])
     * @param btnWarpInstance_jQ jQueryObject
     * @param btnWarpInstance_jQ jQueryObject
     * @param setting HashObject = {
     *     offset: [Number],
     *     callback: [Function]
     *     navItemResumeFn: [Function]
     */
    xf.ScrollSliderNavbar = (function(){
        function ScrollSliderNavbar(btnWarpInstance_jQ, targetWrapInstance_jQ, setting){
            this.btnWrapInstance_jQ = btnWarpInstance_jQ;
            this.targetWrapInstance_jQ = targetWrapInstance_jQ;

            this.offset = setting.offset;
            this.callback = setting.callback;
            this.prevNavItemResumeFn = setting.prevNavItemResumeFn;
            this.node2Stack = new xf.Node2Stack();

            this._init();   //执行初始化方法
        }
        ScrollSliderNavbar.prototype._init = function(){
            var that = this;
            this.btnWrapInstance_jQ.delegate('li>a', 'click', {}, function (event){
                //event.preventDefault();  //消除默认事件
                var $this = $(this),
                    currentScrollTop = $(window).scrollTop(),
                //topic-navbar 高 70px
                    targetScrollTop = that.targetWrapInstance_jQ.find('#' + $this.data('target-id')).offset().top + that.offset,
                    duration = targetScrollTop - currentScrollTop,
                    motion1 = xf.Tween.Motion.createMotionNode();
                //给dom实例注册 resume方法 提供 Node2Stack 调用
                //构造临时 对象
                function target(){
                    this.target = event.target;
                    this.resume = function(){
                        that.prevNavItemResumeFn.call(this);
                    }
                }
                that.node2Stack.push({
                    motion: motion1,
                    target: new target()
                });
                // 初始化 运动 节点
                motion1.initNode({
                    MoverTweenType: xf.Tween.Quad.easeOut,
                    beforeMove:  currentScrollTop,
                    changeDistance: duration,
                    duration: 50
                });
                motion1.regResumeFn(function(){  //注册 Node2Stack 约定的 Resume 方法
                    that.node2Stack.current.motion.off();
                }).on(function(cv){ //开始滚动
                        $(window).scrollTop( cv );
                    }).call(function(){ //滚动完成之后，菜单字体变成 蓝色
                        that.callback(event.target);
                    });
            });
        };
        return ScrollSliderNavbar;
    })();
})(XF || ( XF  = {}  ));